'use strict';

angular.module('afasApp')
  .controller('TeachersCtrl', ['$scope', 'Services', '$state', '$filter', function ($scope, Services, $state, $filter) {

    $scope.teachers = [];
    $scope.searchTypes = [
      {
        'Label': 'All',
        'Query': 'teacher,native_teacher'
      },
      {
        'Label': 'Indonesian Teacher',
        'Query': 'teacher'
      },
      {
        'Label': 'Native Teacher',
        'Query': 'native_teacher'
      }
    ];
    $scope.teacherNew = {
      'Sex': 0
    };
    $scope.errors = {};
    $scope.params = {
      'staff_type': 'teacher,native_teacher'
    };

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('staffs', data)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;

        var items = _.where(results.data, {StaffType: 0});
        $scope.teachers = _.difference(results.data, items) ;
      });
    }
    getResultsPage(1);

    // Services.getItems('staffs', {'getall': true})
    // .then(function(results){
    //   var items = _.where(results.data, {StaffType: 0});
    //   $scope.teachers = _.difference(results.data, items) ;
    // });

    $scope.addTeacher = function () {
      $scope.errors = {};
      $scope.teacherNew.DoB = $filter('date')($scope.timepicker.timeA, 'yyyy-MM-dd');
      Services.add('staffs', $scope.teacherNew)
      .then(function(results){
        // console.log(results);
        // $scope.teacherNew = {};
        $state.go('^.list');
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

  }]).controller('TeacherCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.currentTeacher = {};
    $scope.currentID = $stateParams.id;
    // $scope.sexes = Sexes.getSexes();

    Services.getItem('staffs', $scope.currentID)
    .then(function(results){
      $scope.item = results.data;
    });

    // $scope.currentTeacher =Teachers.getTeacher($stateParams.id);

    $scope.update = function () {
      // Teachers.updateTeacher($scope.currentTeacher, $scope.currentID);
      Services.update('staffs', $scope.item, $scope.currentID)
      .then(function(results){
        console.log(results);
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };

  }]);